package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Weibo;
import cn.tedu.weibo.pojo.vo.*;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/weibo/")
public class WeiboController {
   @Autowired
    WeiboMapper mapper;
   @RequestMapping("/insert")
    int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
       System.out.println("weiboDTO = " + weiboDTO);
       UserVO user=(UserVO)session.getAttribute("userVO");
       if(user==null){
           return 2;
       }
       Weibo weibo=new Weibo();
       BeanUtils.copyProperties(weiboDTO,weibo);
       weibo.setUserId(user.getId());
       weibo.setCreated(new Date());
       System.out.println("weibo = " + weibo);
       mapper.insert(weibo);
       return 1;
   }
   @RequestMapping("/select")
   List<WeiboIndexVO> select(){
      return mapper.select();
   }
   @RequestMapping("/select/mine")
   public ResultVO selectMine(HttpSession session){
      UserVO userVO=(UserVO) session.getAttribute("userVO");
      if(userVO==null){
         return new ResultVO(2,"用户未登录",null);
      }
      return new ResultVO(1,"请求成功",mapper.selectMine(userVO.getId()));
   }
   @RequestMapping("/selectById")
   WeiboDetailVO selectById(int id){
      return mapper.selectById(id);

   }
   @Autowired
   CommentMapper mapper2;
   @RequestMapping("/deleteByWeiboId")
   public void deleteByWeiboId(int id){
      mapper2.deleteByWeiboId(id);
      mapper.deleteByWeiboId(id);


   }

}
